// https://leetcode.cn/problems/simplify-path/
#include <iostream>
#include <string>
#include <vector>
#include <sstream>


using namespace std;

class Solution {
public:
	// vector<string>split(const string & input, const string & regexStr){
		// regex pattern(regexStr);
		// std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),
                                   // std::sregex_token_iterator());
		// return elems;
	// }
	vector<string>splitStr(const string & input, const string & delim){
		string tmp;
		vector<string> res;
		for(int i = 0; i < input.size(); i++){
			if(string(1, input[i]) == delim){
				res.push_back(tmp);
				tmp = "";
			}else{
				tmp += input[i];
			}
		}
		if(tmp != ""){
			res.push_back(tmp);
		}
		return res;
	}

    string simplifyPath(string path) {
		vector<string> pathArr = splitStr(path, "/");
		vector<string> s2;
		for(auto i : pathArr){
			if(i == "" || i == "."){
				continue;
			}
			if(i == ".." && !s2.empty()){
				s2.pop_back();
				continue;
			}
			s2.push_back(i);
		}
		string res = "/";
		for(auto i : s2){
			res += i + "/";
		}
		res = res.substr(0, res.size()-1);
		return res;
    }
	
	string simplifyPath1(string path) {
		vector<string> arr;
		stringstream ss(path);
		string str;
		string res = "/";
		while(getline(ss, str, '/')){
			// cout << str << endl;
			if(str == "" || str == "."){
				continue;
			}
			if(str == ".."){
				if(arr.empty()){
					continue;
				}else{
					arr.pop_back();
				}
			}else{
				arr.push_back(str);
			}
		}
		if(!arr.empty()){
			for(auto i : arr){
				res += i + "/";
			}
			res = res.substr(0, res.size() - 1);
		}
		
		return res;
    }
};

int main(){
	Solution so;
	string path = "/a//b////c/d//././/.."; // a/b/c/d
	string a = so.simplifyPath1(path);
	cout << a << endl;
	// auto s1 = so.splitStr(path, "/");
	
	// cout << "start:" << endl;
	
	// for(auto i : s1){
		// cout << i << endl;
	// }
	return 0;
}